import * as THREE from 'three'
import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer'
import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass'
import { OutlinePass } from 'three/examples/jsm/postprocessing/OutlinePass'
import { ShaderPass } from 'three/examples/jsm/postprocessing/ShaderPass'
import { FXAAShader } from 'three/examples/jsm/shaders/FXAAShader'

export default class Composer {
  constructor(_viewer) {
    this.viewer = _viewer
    this.composer = new EffectComposer(this.viewer.renderer)
    const renderPass = new RenderPass(this.viewer.scene, this.viewer.camera)
    this.composer.addPass(renderPass)

    this.getOutlinePass('run', 0x15c5e8) // 运行
    this.getOutlinePass('alarmLevel0', '#ffc90e') // 一般告警
    this.getOutlinePass('alarmLevel1', '#ff9000') // 严重告警
    this.getOutlinePass('alarmLevel2', '#e00707') // 致命告警

    const effectFXAA = new ShaderPass(FXAAShader)
    effectFXAA.uniforms.resolution.value.set(1 / this.viewer.renderer.domElement.clientWidth, 1 / this.viewer.renderer.domElement.clientHeight)
    this.composer.addPass(effectFXAA)

    this.composer.renderTarget1.texture.encoding = THREE.sRGBEncoding
    this.composer.renderTarget2.texture.encoding = THREE.sRGBEncoding

    this.viewer.composer = this.composer
  }

  getOutlinePass(key, color) {
    const outlinePass = new OutlinePass(new THREE.Vector2(this.viewer.renderer.domElement.clientWidth, this.viewer.renderer.domElement.clientHeight), this.viewer.scene, this.viewer.camera)
    outlinePass.edgeStrength = 10.0 // 边框的亮度
    outlinePass.edgeGlow = 2 // 光晕
    outlinePass.edgeThickness = 4.0 // 边框宽度
    outlinePass.downSampleRatio = 1 // 边框弯曲度
    outlinePass.pulsePeriod = 3 // 呼吸闪烁的速度
    outlinePass.visibleEdgeColor.set(color) // 呼吸颜色
    outlinePass.hiddenEdgeColor.set(color) // 呼吸消失的颜色
    this.composer[key] = outlinePass
    this.composer.addPass(outlinePass)
  }
}
